package com.zxl.always.on.display.model.bean;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.constant.ICustomAction;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.ui.service.ScreenService;

/**
 * Created by Administrator on 2018/3/6.
 */

public class ClockInfo {

    public ClockStyle clockStyle;

    public int palettePosition;

    public int backgroundPosition;

    public int clockPosition;

    public ClockInfo(ClockStyle clockStyle) {
        this.clockStyle = clockStyle;
        SharedPreferences sharedPreferences = getSharedPreferences(clockStyle);
        palettePosition = sharedPreferences.getInt("palette", 0);
        backgroundPosition = sharedPreferences.getInt("background", 0);
        clockPosition = sharedPreferences.getInt("clock", 0);
    }

    public ClockInfo(ClockStyle clockStyle, int palettePosition, int backgroundPosition, int clockPosition) {
        this.clockStyle = clockStyle;
        this.palettePosition = palettePosition;
        this.backgroundPosition = backgroundPosition;
        this.clockPosition = clockPosition;
    }

    public void saveData() {
        getSharedPreferences(clockStyle).edit().putInt("style", clockStyle.getId())
                .putInt("palette", palettePosition).putInt("background", backgroundPosition).putInt("clock", clockPosition).commit();

        getSharedPreferences().edit().putInt("style", clockStyle.getId()).commit();

        Intent intent = new Intent(ApplicationHelper.getContext(), ScreenService.class);
        intent.setAction(ICustomAction.ACTION_CLOCK_STYLE);
        ApplicationHelper.getContext().startService(intent);
    }

    public static ClockStyle getClockStyle() {
        SharedPreferences sharedPreferences = getSharedPreferences();
        int id = sharedPreferences.getInt("style", 0);
        for (ClockStyle clockStyle : ClockStyle.values()) {
            if (clockStyle.getId() == id) {
                return clockStyle;
            }
        }
        return ClockStyle.CLOCK_DIGITAL;
    }

    public static SharedPreferences getSharedPreferences(ClockStyle clockStyle) {
        return ApplicationHelper.getContext().getSharedPreferences("clock_settings_" + clockStyle.getId(), Context.MODE_MULTI_PROCESS);
    }

    private static SharedPreferences getSharedPreferences() {
        return ApplicationHelper.getContext().getSharedPreferences("clock_settings_", Context.MODE_MULTI_PROCESS);
    }

}
